﻿using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp2.Report;

namespace WpfApp2.Report
{
    /// <summary>
    /// WindowReport.xaml 的交互逻辑
    /// </summary>
    public partial class WindowReport : Window
    {
        public WindowReport()
        {
            InitializeComponent();
        }

       

        private string report_wenjianming { get; set; }

        private List<Orders> orders { get; set; }

        private string dt_set { get; set; }

        private List<ReportParameter> pr { get; set; }

        private string printerName { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            show(orders, dt_set, pr,printerName);

        }


        public void init(string title, string report_wenjianming1, List<Orders> orders1, string dt_set1, List<ReportParameter> pr1,string printerName1)
        {
            this.Title = title;
            report_wenjianming = report_wenjianming1;
            orders = orders1;
            dt_set = dt_set1;
            pr = pr1;
            printerName = printerName1;

        }


        private void show(List<Orders> orders, string dataset, List<ReportParameter> pr,string printerName)
        {


           
            reportViewer.LocalReport.DataSources.Clear();

            string path = Environment.CurrentDirectory;

            reportViewer.LocalReport.ReportPath = path + @"\Report\" + report_wenjianming;

            reportViewer.LocalReport.DataSources.Add(new ReportDataSource(dataset, orders));

            if (pr != null)
            {
                for (int i = 0; i < pr.Count; i++)
                {
                    try
                    {
                        reportViewer.LocalReport.SetParameters(pr[i]);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("error:"+ex.Message);
                    }
                }
            }
            
            reportViewer.RefreshReport();

            reportViewer.ZoomMode = ZoomMode.Percent;
            reportViewer.ZoomPercent = 100;

             

            Print(printerName);


        }

        

        /// <summary>

        /// 打印预处理

        /// </summary>

        private void Print(string printerName)

        {

            PrintDocument printDoc = new PrintDocument();

             
            

            printDoc.PrinterSettings.PrinterName = printerName;

            if (!printDoc.PrinterSettings.IsValid)

            {

                string msg = String.Format("Can't find printer \"{0}\".", printerName);

                
                MessageBox.Show(msg);

                return;

            }

             
            StandardPrintController spc = new StandardPrintController();

            printDoc.PrintController = spc;

            printDoc.Print();

        }


    }





}

